package com.example.kotlin.activity

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.kotlin.Msg
import com.example.kotlin.MsgAdapter
import com.example.kotlin.R
import kotlinx.android.synthetic.main.activity_second.inputText
import kotlinx.android.synthetic.main.activity_second.recyclerview1
import kotlinx.android.synthetic.main.activity_second.send

class SecondActivity : BaseActivity(), View.OnClickListener {

    private val msgList=ArrayList<Msg>()

    private lateinit var adapter: MsgAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        initMsg()
        recyclerview1.layoutManager=LinearLayoutManager(this)
        adapter= MsgAdapter(msgList)
        recyclerview1.adapter=adapter
        send.setOnClickListener(this)


    }

    private fun initMsg() {
        val msg1= Msg("Hello guy", Msg.TYPE_RECEIVED)
        msgList.add(msg1)

        val msg2= Msg("Hello .Who is what?", Msg.TYPE_SENT)
        msgList.add(msg2)

        val msg3= Msg("Nice to meet you", Msg.TYPE_RECEIVED)
        msgList.add(msg3)

    }

    override fun onClick(view: View?) {
        when(view){
            send->{
                val content = inputText.text.toString()
                if (content.isNotEmpty()){
                    msgList.add(Msg(content, Msg.TYPE_SENT))
                    adapter.notifyItemInserted(msgList.size-1)
                    recyclerview1.scrollToPosition(msgList.size-1)
                    inputText.setText("")
                }
            }
        }
    }
}